package com.wrench.elevator.tida.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.ZZGLDao;
import com.wrench.elevator.tida.domain.Building;
import com.wrench.elevator.tida.domain.City;
import com.wrench.elevator.tida.domain.District;
import com.wrench.elevator.tida.domain.FlowDetails;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.domain.Province;
import com.wrench.elevator.tida.domain.User;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.util.WrenchUtil;

@Component
public class ZZGLService {
    private static final Logger logger = LoggerFactory.getLogger(ZZGLService.class);

    @Resource
    private ZZGLDao dao;


    public void check(String id, String type) throws WrenchException {
        if (WrenchUtil.hasNull(id, type)) {
            logger.error("==> param is not found , id = {} , type = {} ", id, type);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        int commit = -1;
        try {
            commit = dao.check(id, type);
        } catch (Exception e) {
            logger.error("==>  commit=dao.check(id,type) execute error , id = {} , type = {} , error = {} ", id, type, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }


    }

    public Page  getCheckList(String wuye_id, String building_id, String[] type,String current_page,String limit) throws WrenchException {
        if (WrenchUtil.hasNull(wuye_id)) {
            logger.error("==> param is not found , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if(null==type || type.length<=0||(building_id!=null && building_id.trim().length()==0)){
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        Page page=new Page();
        int int_current_page=Integer.valueOf(current_page);
        int int_limit=Integer.valueOf(limit);
        List<Map> checkList = null;
        try {
            checkList = dao.getCheckList(wuye_id, building_id, type,(int_current_page-1)*int_limit,int_limit);
        } catch (Exception e) {
            logger.error("==> checkList=dao.getCheckList(wuye_id,building_id,type) execute error , checkList = {} , wuye_id = {} , building_id = {} , type = {} , error = {} ", checkList, wuye_id, building_id, type, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        int total=-1;
        try {
            total=dao.getCheckListCount(wuye_id, building_id, type);
        } catch (Exception e) {
           logger.error("==> total=dao.getCheckListCount(wuye_id, building_id, type) execute error , total = {} , error = {} ",total,e);
           throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }
        page.setTotal(total);
        page.setList(checkList);
        page.setLimit(int_limit);
        page.setCurrent_page(int_current_page);

        return page;

    }


    public List<Province> getProvinceByWuyeId(String wuye_id)
            throws WrenchException {
        if (WrenchUtil.hasNull(wuye_id)) {
            logger.error("==> param is not found , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.isInt(wuye_id)) {
            logger.error("==> string is not int type , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<Province> provinces = null;
        try {
            provinces = dao.getProvinceByWuyeId(Integer.valueOf(wuye_id));
        } catch (Exception e) {
            logger.error("==> provinces = dao.getProvinceByWuyeId(Integer.valueOf(wuye_id)) execute error , wuye_id = {} , error = {} ",
                    wuye_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(provinces)) {
            logger.error("==> provinces = dao.getProvinceByWuyeId(Integer.valueOf(wuye_id)) execute success but districts is null, wuye_id = {} , provinces = {} ",
                    wuye_id,
                    provinces);
            throw new WrenchException(Result.GET_DISTRICT_BY_WUYE_ID_FAIL_CODE,
                    Result.GET_DISTRICT_BY_WUYE_ID_FAIL_MESSAGE);

        }

        logger.debug("==> provinces = dao.getProvinceByWuyeId(Integer.valueOf(wuye_id)) execute success , wuye_id = {} , districts = {}  ",
                wuye_id,
                provinces);
        return provinces;

    }

    public List<District> getDistrictByWuyeId(String wuye_id)
            throws WrenchException {
        if (WrenchUtil.hasNull(wuye_id)) {
            logger.error("==> param is not found , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.isInt(wuye_id)) {
            logger.error("==> string is not int type , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<District> districts = null;
        try {
            districts = dao.getDistrictByWuyeId(Integer.valueOf(wuye_id));
        } catch (Exception e) {
            logger.error("==> districts=dao.getDistrictByWuyeId(Integer.valueOf(wuye_id)) execute error , wuye_id = {} , error = {} ",
                    wuye_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(districts)) {
            logger.error("==> districts=dao.getDistrictByWuyeId(Integer.valueOf(wuye_id)) execute success but districts is null, wuye_id = {} , districts = {} ",
                    wuye_id,
                    districts);
            throw new WrenchException(Result.GET_DISTRICT_BY_WUYE_ID_FAIL_CODE,
                    Result.GET_DISTRICT_BY_WUYE_ID_FAIL_MESSAGE);

        }

        logger.debug("==> districts=dao.getDistrictByWuyeId(Integer.valueOf(wuye_id)) execute success , wuye_id = {} , districts = {}  ",
                wuye_id,
                districts);
        return districts;

    }

    public List<Province> getProvinces(String wuye_id)
            throws WrenchException {

        if (WrenchUtil.hasNull(wuye_id)) {
            logger.error("==> param is not found , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.isInt(wuye_id)) {
            logger.error("==> string is not int type , wuye_id = {} ", wuye_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        List<Province> provinces = null;
        try {
            provinces = dao.getProvinces(Integer.valueOf(wuye_id));
        } catch (Exception e) {
            logger.error("==> provinces=dao.getProvinces() execute error , error = {} ", e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(provinces)) {
            logger.error("==> provinces=dao.getProvinces() execute success , but province is null, provinces = {}  ",
                    provinces);
            throw new WrenchException(Result.GET_PROVINCES_FAIL_CODE, Result.GET_PROVINCES_FAIL_MESSAGE);
        }
        logger.debug("==> provinces=dao.getProvinces() execute success , provinces = {}  ", provinces);
        return provinces;
    }

    public List<City> getCitys(String wuye_id, String province_id)
            throws WrenchException {

        if (WrenchUtil.hasNull(wuye_id, province_id)) {
            logger.error("==> param is not found , wuye_id = {} , province_id = {} ", wuye_id, province_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(wuye_id, province_id)) {
            logger.error("==> string is not int type , wuye_id = {} , province_id = {} ", wuye_id, province_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<City> citys = null;
        try {
            citys = dao.getCitys(Integer.valueOf(wuye_id), Integer.valueOf(province_id));
        } catch (Exception e) {
            logger.error("==> citys = dao.getCitys(Integer.valueOf(province_id)) execute error , province_id = {} , error = {} ",
                    province_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(citys)) {
            logger.error("==> citys = dao.getCitys(Integer.valueOf(province_id)) execute success , but citys is null, citys = {} ,province_id = {}  ",
                    citys,
                    province_id);
            throw new WrenchException(Result.GET_CITYS_FAIL_CODE, Result.GET_CITYS_FAIL_MESSAGE);
        }
        logger.debug("==> citys = dao.getCitys(Integer.valueOf(province_id)) execute success ,  citys = {} ,province_id = {}   ",
                citys,
                province_id);
        return citys;
    }

    public List<District> getDistricts(String wuye_id, String city_id)
            throws WrenchException {

        if (WrenchUtil.hasNull(wuye_id, city_id)) {
            logger.error("==> param is not found , wuye_id = {}, city_id = {} ", wuye_id, city_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(wuye_id, city_id)) {
            logger.error("==> string is not int type , wuye_id = {} , city_id = {} ", wuye_id, city_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<District> districts = null;
        try {
            districts = dao.getDistricts(Integer.valueOf(wuye_id), Integer.valueOf(city_id));
        } catch (Exception e) {
            logger.error("==> districts = dao.getDistricts(Integer.valueOf(city_id))) execute error , city_id = {} , error = {} ",
                    city_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(districts)) {
            logger.error("==> districts = dao.getDistricts(Integer.valueOf(city_id))) execute success , but citys is null, citys = {} ,city_id = {}  ",
                    districts,
                    city_id);
            throw new WrenchException(Result.GET_DISTRICTS_FAIL_CODE, Result.GET_DISTRICTS_FAIL_MESSAGE);
        }
        logger.debug("==> districts = dao.getDistricts(Integer.valueOf(city_id))) execute success ,  citys = {} ,city_id = {}   ",
                districts,
                city_id);
        return districts;
    }

    public List<Building> getBuildings(String district_id)
            throws WrenchException {

        if (WrenchUtil.hasNull(district_id)) {
            logger.error("==> param is not found , district_id = {} ", district_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.isInt(district_id)) {
            logger.error("==> string is not int type , district_id = {} ", district_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<Building> building = null;
        try {
            building = dao.getBuildings(Integer.valueOf(district_id));
        } catch (Exception e) {
            logger.error("==> building = dao.getBuildings(Integer.valueOf(district_id))) execute error , district_id = {} , error = {} ",
                    district_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(building)) {
            logger.error("==> districts = dao.getDistricts(Integer.valueOf(city_id))) execute success , but citys is null, building = {} ,district_id = {}  ",
                    building,
                    district_id);
            throw new WrenchException(Result.GET_BUILDINGS_FAIL_CODE, Result.GET_BUILDINGS_FAIL_MESSAGE);
        }
        logger.debug("==> districts = dao.getDistricts(Integer.valueOf(city_id))) execute success ,  building = {} ,district_id = {}   ",
                building,
                district_id);
        return building;
    }


    public Map getBuildingInfo (String building_id) throws WrenchException {
        if(WrenchUtil.hasNull(building_id)){
            logger.error("==> param is not found , building_id = {} ",building_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        Map building=null;
        try {
            building=dao.getBuildingInfo(building_id);
        } catch (Exception e) {
           logger.error("==> building=dao.getBuildingInfo(building_id) execute error , building_id = {} , error = {} ",building_id,e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        return building;
    }


    public int alterBuilding(String building_id,String building_name,String price, String reg_charge, String is_check, String building_type,String chengti_type,String reg_gift)
            throws WrenchException {
        if (WrenchUtil.hasNull(building_id,building_name,
               price, reg_charge, is_check, building_type,chengti_type)) {
            logger.error("==> param is not found , building_id = {} , building_name = {} , price = {} , reg_charge = {} , is_check = {}  , building_type = {}  ,chengti_type = {}",
                   building_id, building_name,
                 price, reg_charge, is_check, building_type,chengti_type);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }


        Building building = new Building();
        building.setBuilding_name(building_name);
        building.setPrice(price);
        building.setReg_charge(reg_charge);
        building.setIs_check(is_check);
        building.setBuilding_type(building_type);
        building.setChengti_type(chengti_type);
        building.setReg_gift(reg_gift);
        building.setBuilding_id(building_id);

        int commit = -1;
        try {
            commit = dao.alterBuilding(building);
        } catch (Exception e) {
            logger.error("==> commit=dao.alterBuilding(building) execute error , building = {} ,error = {} ", building, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (commit <= 0) {
            logger.error("==> commit=dao.alterBuilding(building) execute success , but commit<=0, building = {} , commit = {} ",
                    building,
                    commit);
            throw new WrenchException(Result.ADD_BUILDING_FAIL_CODE, Result.ADD_BUILDING_FAIL_MESSAGE);
        }
        return commit;
    }


    public int addBuilding(String building_name, String province_id, String province_name, String city_id,
                           String city_name, String district_id, String district_name, String price, String reg_charge, String is_check, String building_type,String chengti_type,String reg_gift)
            throws WrenchException {
        if (WrenchUtil.hasNull(building_name,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name, price, reg_charge, is_check, building_type,chengti_type,reg_gift)) {
            logger.error("==> param is not found , building_name = {} , province_id = {}, province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , price = {} , reg_charge = {} , is_check = {}  , building_type = {}  ,chengti_type = {} , reg_gift = {} ",
                    building_name,
                    province_id,
                    province_name,
                    city_id,
                    city_name,
                    district_id,
                    district_name, price, reg_charge, is_check, building_type,chengti_type,reg_gift);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(province_id, city_id, district_id)) {
            logger.error("==> string is not int type ,  building_name = {} , province_id = {}, province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} ",
                    building_name,
                    province_id,
                    province_name,
                    city_id,
                    city_name,
                    district_id,
                    district_name);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        Building building = new Building();
        building.setBuilding_name(building_name);
        building.setProvince_id(province_id);
        building.setProvince_name(province_name);
        building.setCity_id(city_id);
        building.setCity_name(city_name);
        building.setDistrict_id(district_id);
        building.setDistrict_name(district_name);
        building.setPrice(price);
        building.setReg_charge(reg_charge);
        building.setIs_check(is_check);
        building.setBuilding_type(building_type);
    building.setChengti_type(chengti_type);
    building.setReg_gift(reg_gift);


        int commit = -1;
        try {
            commit = dao.addBuilding(building);
        } catch (Exception e) {
            logger.error("==> commit=dao.addBuilding(building) execute error , building = {} ,error = {} ", building, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (commit <= 0) {
            logger.error("==> commit=dao.addBuilding(building) execute success , but commit<=0, building = {} , commit = {} ",
                    building,
                    commit);
            throw new WrenchException(Result.ADD_BUILDING_FAIL_CODE, Result.ADD_BUILDING_FAIL_MESSAGE);
        }
        return commit;
    }

    public void inputUser(String danyuan_id, String loucen, String menpaihao, String user_tel, String balance,
                          ModelMap map)
            throws WrenchException {
        if (WrenchUtil.hasNull(danyuan_id, loucen, menpaihao, user_tel, balance)) {
            logger.error("==> param is not found , danyuan_id = {} , loucen = {} , menpaihao = {} , user_tel = {} , balance = {}  ",
                    danyuan_id,
                    loucen,
                    menpaihao,
                    user_tel,
                    balance);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.isVaildPhone(user_tel)) {
            logger.error("==> telphone is not vaild , user_tel = {} ", user_tel);
            throw new WrenchException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }

        if (!WrenchUtil.areInt(loucen)) {
            logger.error("==> string is not int type , loucen = {} ", loucen);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        if (!WrenchUtil.isMoneyStr(balance)) {
            logger.error("==> string is not money , balance = {} ", balance);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }

        User user = new User("", danyuan_id, loucen, menpaihao, user_tel, Double.valueOf(balance));

        WrenchUtil.getParamMap(map, "user", user);

        try {
            dao.inputUser(map);
        } catch (Exception e) {
            logger.error("==> dao.inputUser(map) execute error , user = {} , error = {}", user, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

    }

    public Page queryUserByProvinceId(String province_id,String wuye_id, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(province_id, wuye_id,current_page, limit)) {
            logger.error("==> param is not found , province_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    province_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit)) {
            logger.error("==> string is not int type , current_apge = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_MESSAGE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByProvinceId(province_id,wuye_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit));
        } catch (Exception e) {
            logger.error("==> userList=dao.queryUserByProvinceId(building_id,wuye_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , province_id = {} , wuye_id = {} , current_page = {} , limit = {} , error = {} ",
                    province_id,
                    wuye_id,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUserByProvinceId(province_id, wuye_id,(Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success , but userList is null , userList = {}  , province_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    userList,
                    province_id,
                    wuye_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.QUERY_USER_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_DANYUAN_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUserByProvinceId(province_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  , userList = {}  , province_id = {} , current_page = {} , limit = {} , ",
                userList,
                province_id,
                wuye_id,
                current_page,
                limit);

        int total = -1;

        try {
            total = dao.queryUserByProvinceIdCount(province_id,wuye_id);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserByProvinceIdCount(province_id,wuye_id) execute error , province_id = {} , wuye_id = {} , error = {} ",
                    province_id,
                    wuye_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserByProvinceIdCount(province_id,wuye_id) execute success but total<0 , total = {} , province_id = {} , wuye_id = {} ",
                    total,
                    province_id,wuye_id);
            throw new WrenchException(Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_MESSAGE);
        }
        logger.debug("==> total=dao.queryUserByProvinceIdCount(province_id,wuye_id) execute success , total = {} , province_id = {} , wuye_id = {}",
                total,
                province_id,wuye_id);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }

    public Page queryUserByBuildingId(String building_id, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(building_id, current_page, limit)) {
            logger.error("==> param is not found , building_id = {} ,current_page = {} , limit = {} ",
                    building_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit)) {
            logger.error("==> string is not int type , current_apge = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_MESSAGE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByBuildingId(building_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit));
        } catch (Exception e) {
            logger.error("==> userList=dao.queryUserByBuildingId(building_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , building_id = {} , current_page = {} , limit = {} , error = {} ",
                    building_id,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUserByBuildingId(building_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success , but userList is null , userList = {}  , building_id = {} , current_page = {} , limit = {} ",
                    userList,
                    building_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.QUERY_USER_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_DANYUAN_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUserByBuildingId(building_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  , userList = {}  , building_id = {} , current_page = {} , limit = {} , ",
                userList,
                building_id,
                current_page,
                limit);

        int total = -1;

        try {
            total = dao.queryUserByBuildingIdCount(building_id);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserBuildingIdCount(danyuan_id) execute error , building_id = {} , error = {} ",
                    building_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserBuildingIdCount(danyuan_id) execute success but total<0 , total = {} , building_id = {} ",
                    total,
                    building_id);
            throw new WrenchException(Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_MESSAGE);
        }
        logger.debug("==> total=dao.queryUserBuildingIdCount(danyuan_id) execute success , total = {} , building_id = {} ",
                total,
                building_id);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }



    public Page queryUserByDanyuanId(String danyuan_id, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(danyuan_id, current_page, limit)) {
            logger.error("==> param is not found , danyuan_id = {} ,current_page = {} , limit = {} ",
                    danyuan_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit)) {
            logger.error("==> string is not int type , current_apge = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_MESSAGE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByDanyuanId(danyuan_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit));
        } catch (Exception e) {
            logger.error("==> userList=dao.queryUser(danyuan_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , danyuan_id = {} , current_page = {} , limit = {} , error = {} ",
                    danyuan_id,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUser(danyuan_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success , but userList is null , userList = {}  , danyuan_id = {} , current_page = {} , limit = {} ",
                    userList,
                    danyuan_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.QUERY_USER_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_DANYUAN_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUser(danyuan_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  , userList = {}  , danyuan_id = {} , current_page = {} , limit = {} , ",
                userList,
                danyuan_id,
                current_page,
                limit);

        int total = -1;

        try {
            total = dao.queryUserByDanyuanIdCount(danyuan_id);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserByDanyuanIdCount(danyuan_id) execute error , danyuan_id = {} , error = {} ",
                    danyuan_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserByDanyuanIdCount(danyuan_id) execute success but total<0 , total = {} , danyuan_id = {} ",
                    total,
                    danyuan_id);
            throw new WrenchException(Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_MESSAGE);
        }
        logger.debug("==> total=dao.queryUserByDanyuanIdCount(danyuan_id) execute success , total = {} , danyuan_id = {} ",
                total,
                danyuan_id);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }

    public int updateUser(String loucen, String menpaihao, String user_id, String balance)
            throws WrenchException {
        if (WrenchUtil.hasNull(loucen, menpaihao, user_id, balance)) {
            logger.error("==> param is not found , loucen = {} , menpaihao = {} , user_id = {} , balance = {} ",
                    loucen,
                    menpaihao,
                    user_id,
                    balance);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.isInt(loucen)) {
            logger.error("==> string is not int type , loucen = {} ", loucen);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        if (!WrenchUtil.isMoneyStr(balance)) {
            logger.error("==> string is not money , balance = {}", balance);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }
        User user = new User();
        user.setBalance(Double.valueOf(balance));
        user.setLoucen(loucen);
        user.setMenpaihao(menpaihao);
        user.setUser_id(user_id);

        int commit = -1;
        try {
            commit = dao.updateUser(user);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("==> commit=dao.updateUser(user) execute error , user = {} , error = {} ", user, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (commit <= 0) {
            logger.error("==> commit=dao.updateUser(user) execute success but commit<=0 , user = {} , commit = {} ",
                    user,
                    commit);
            throw new WrenchException(Result.UPDATE_USER_FAIL_CODE, Result.UPDATE_USER_FAIL_MESSAGE);
        }
        return commit;

    }

    public Page queryUserByTelAndWuyeId(String wuye_id, String user_tel, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(wuye_id, user_tel, current_page, limit)) {
            logger.error("==> param is not found , wuye_id = {} , user_tel = {} , current_page = {} , limit = {} ",
                    wuye_id,
                    user_tel,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.areInt(wuye_id, current_page, limit)) {
            logger.error("==> string is not int type ,  wuye_id = {} , current_page = {} , limit = {} ",
                    wuye_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByTelAndWuyeId(Integer.valueOf(wuye_id), user_tel, (Integer.valueOf(current_page) - 1)
                            * Integer.valueOf(limit), Integer.valueOf(limit));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("==> userList=dao.queryUserByTelAndWuyeId(Integer.valueOf(wuye_id), user_tel, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error ,  wuye_id = {} , user_tel = {} , current_page = {} , limit = {} , error = {} ",
                    wuye_id,
                    user_tel,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUserByTelAndWuyeId(Integer.valueOf(wuye_id), user_tel, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success but userList is null ,  wuye_id = {} , user_tel = {} , current_page = {} , limit = {} , userList = {} ",
                    wuye_id,
                    user_tel,
                    current_page,
                    limit,
                    userList);
            throw new WrenchException(Result.QUERY_USER_BY_TEL_AND_WUYE_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_TEL_AND_WUYE_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUserByTelAndWuyeId(Integer.valueOf(wuye_id), user_tel, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  ,  wuye_id = {} , user_tel = {} , current_page = {} , limit = {} , userList = {} ",
                wuye_id,
                user_tel,
                current_page,
                limit,
                userList);
        int total = -1;

        try {
            total = dao.queryUserByTelAndWuyeIdCount(Integer.valueOf(wuye_id), user_tel);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserByTelAndWuyeIdCount(Integer.valueOf(wuye_id), user_tel) execute error , wuye_id = {} , user_tel = {} ",
                    wuye_id,
                    user_tel);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserByTelAndWuyeIdCount(Integer.valueOf(wuye_id), user_tel) execute success , but total <0 ,  wuye_id = {} , user_tel = {}  , total = {} ",
                    wuye_id,
                    user_tel,
                    total);
            throw new WrenchException(Result.QUERY_USER_BY_TEL_AND_WUYE_ID_COUNT_FAIL_CODE,
                    Result.QUERY_USER_BY_TEL_AND_WUYE_ID_COUNT_FAIL_MESSAGE);
        }
        logger.error("==> total=dao.queryUserByTelAndWuyeIdCount(Integer.valueOf(wuye_id), user_tel) execute success  ,  wuye_id = {} , user_tel = {}  , total = {} ",
                wuye_id,
                user_tel,
                total);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }

    public Page getChongzhiDetails(String user_id, String current_page, String limit, String start_date, String end_date)
            throws WrenchException {
        if (WrenchUtil.hasNull(user_id, current_page, limit, start_date, end_date)) {
            logger.error("==> param is not found , user_id = {} , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit, start_date, end_date)) {
            logger.error("==> string is not int type , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<FlowDetails> flowDetailsList = null;
        try {
            flowDetailsList =
                    dao.getChongzhiDetails(user_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit),
                            Integer.valueOf(start_date),
                            Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> flowDetailsList=dao.getChongzhiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit),Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} ,error = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(flowDetailsList)) {
            logger.error("==> flowDetailsList=dao.getChongzhiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success but  flowDetailsList is null ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    flowDetailsList);
            throw new WrenchException(Result.GET_CHONGZHI_DETAILS_FAIL_CODE, Result.GET_CHONGZHI_DETAILS_FAIL_MESSAGE);

        }
        logger.debug("==> flowDetailsList=dao.getChongzhiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                user_id,
                current_page,
                limit,
                start_date,
                end_date,
                flowDetailsList);

        int total = -1;
        try {
            total = dao.getChongzhiDetailsCount(user_id, Integer.valueOf(start_date), Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> total=dao.getChongzhiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error , user_id = {} , start_date = {} , end_date = {} , error = {} ",
                    user_id,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.getChongzhiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success but total <0 , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                    user_id,
                    total);
            throw new WrenchException(Result.GET_CHONGZHI_DETAILS_COUNT_FAIL_CODE,
                    Result.GET_CHONGZHI_DETAILS_COUNT_FAIL_MESSAGE);
        }

        logger.error("==> total=dao.getChongzhiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                user_id,
                total);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(flowDetailsList);
        return page;

    }

    public Page getChengtiDetails(String user_id, String current_page, String limit, String start_date, String end_date)
            throws WrenchException {
        if (WrenchUtil.hasNull(user_id, current_page, limit, start_date, end_date)) {
            logger.error("==> param is not found , user_id = {} , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit, start_date, end_date)) {
            logger.error("==> string is not int type , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<FlowDetails> flowDetailsList = null;
        try {
            flowDetailsList =
                    dao.getChengtiDetails(user_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit),
                            Integer.valueOf(start_date),
                            Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> flowDetailsList=dao.getChengtiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit),Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} ,error = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(flowDetailsList)) {
            logger.error("==> flowDetailsList=dao.getChengtiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success but  flowDetailsList is null ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    flowDetailsList);
            throw new WrenchException(Result.GET_CHENGTI_DETAILS_FAIL_CODE, Result.GET_CHENGTI_DETAILS_FAIL_MESSAGE);

        }
        logger.debug("==> flowDetailsList=dao.getChengtiDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                user_id,
                current_page,
                limit,
                start_date,
                end_date,
                flowDetailsList);

        int total = -1;
        try {
            total = dao.getChengtiDetailsCount(user_id, Integer.valueOf(start_date), Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> total=dao.getChengtiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error , user_id = {} , start_date = {} , end_date = {} , error = {} ",
                    user_id,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.getChengtiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success but total <0 , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                    user_id,
                    total);
            throw new WrenchException(Result.GET_CHENGTI_DETAILS_COUNT_FAIL_CODE,
                    Result.GET_CHENGTI_DETAILS_COUNT_FAIL_MESSAGE);
        }

        logger.error("==> total=dao.getChengtiDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                user_id,
                total);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(flowDetailsList);
        return page;
    }

    public Page getFlowDetails(String user_id, String current_page, String limit, String start_date, String end_date)
            throws WrenchException {
        if (WrenchUtil.hasNull(user_id, current_page, limit, start_date, end_date)) {
            logger.error("==> param is not found , user_id = {} , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit, start_date, end_date)) {
            logger.error("==> string is not int type , current_page = {} , limit = {} , start_date = {} , end_date = {} ",
                    current_page,
                    limit,
                    start_date,
                    end_date);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<FlowDetails> flowDetailsList = null;
        try {
            flowDetailsList =
                    dao.getFlowDetails(user_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit),
                            Integer.valueOf(start_date),
                            Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> flowDetailsList=dao.getFlowDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit),Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} ,error = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(flowDetailsList)) {
            logger.error("==> flowDetailsList=dao.getFlowDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success but  flowDetailsList is null ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                    user_id,
                    current_page,
                    limit,
                    start_date,
                    end_date,
                    flowDetailsList);
            throw new WrenchException(Result.GET_CHONGZHI_DETAILS_FAIL_CODE, Result.GET_CHONGZHI_DETAILS_FAIL_MESSAGE);

        }
        logger.debug("==> flowDetailsList=dao.getFlowDetails(user_id,( Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success ,user_id = {} , current_page = {} , limit = {},  start_date = {} , end_date = {} , flowDetailsList = {}  ",
                user_id,
                current_page,
                limit,
                start_date,
                end_date,
                flowDetailsList);

        int total = -1;
        try {
            total = dao.getFlowDetailsCount(user_id, Integer.valueOf(start_date), Integer.valueOf(end_date));
        } catch (Exception e) {
            logger.error("==> total=dao.getFlowDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute error , user_id = {} , start_date = {} , end_date = {} , error = {} ",
                    user_id,
                    start_date,
                    end_date,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.getFlowDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success but total <0 , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                    user_id,
                    total);
            throw new WrenchException(Result.GET_FLOW_DETAILS_COUNT_FAIL_CODE,
                    Result.GET_FLOW_DETAILS_COUNT_FAIL_MESSAGE);
        }

        logger.error("==> total=dao.getFlowDetailsCount(user_id,Integer.valueOf(start_date),Integer.valueOf(end_date)) execute success , user_id = {} , start_date = {} , end_date = {} , total = {}  ",
                user_id,
                total);

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(flowDetailsList);
        return page;
    }



    public Page queryUserByCityId(String city_id,String wuye_id, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(city_id, wuye_id,current_page, limit)) {
            logger.error("==> param is not found , city_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    city_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit)) {
            logger.error("==> string is not int type , current_apge = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_MESSAGE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByCityId(city_id,wuye_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit));
        } catch (Exception e) {
            logger.error("==> userList=dao.queryUserByCityId(building_id,wuye_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , city_id = {} , wuye_id = {} , current_page = {} , limit = {} , error = {} ",
                    city_id,
                    wuye_id,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUserByCityId(city_id, wuye_id,(Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success , but userList is null , userList = {}  , city_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    userList,
                    city_id,
                    wuye_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.QUERY_USER_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_DANYUAN_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUserByCityId(city_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  , userList = {}  , province_id = {} , current_page = {} , limit = {} , ",
                userList,
                city_id,
                wuye_id,
                current_page,
                limit);

        int total = -1;

        try {
            total = dao.queryUserByCityIdCount(city_id,wuye_id);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserByCityIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {} , error = {} ",
                    city_id,
                    wuye_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserByCityIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {} ",
                    city_id,
                    wuye_id);
            throw new WrenchException(Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_MESSAGE);
        }
        logger.error("==> total=dao.queryUserByCityIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {}  ",
                city_id,
                wuye_id
                );

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }



    public Page queryUserByDistrictId(String district_id,String wuye_id, String current_page, String limit)
            throws WrenchException {
        if (WrenchUtil.hasNull(district_id, wuye_id,current_page, limit)) {
            logger.error("==> param is not found , district_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    district_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (!WrenchUtil.areInt(current_page, limit)) {
            logger.error("==> string is not int type , current_apge = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_MESSAGE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }

        List<User> userList = null;
        try {
            userList =
                    dao.queryUserByDistrictId(district_id,wuye_id,
                            (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                            Integer.valueOf(limit));
        } catch (Exception e) {
            logger.error("==> userList=dao.queryUserByDistrictId(district_id,wuye_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute error , district_id = {} , wuye_id = {} , current_page = {} , limit = {} , error = {} ",
                    district_id,
                    wuye_id,
                    current_page,
                    limit,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(userList)) {
            logger.error("==> userList=dao.queryUserByDistrictId(district_id, wuye_id,(Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success , but userList is null , userList = {}  , district_id = {} , wuye_id = {} , current_page = {} , limit = {} ",
                    userList,
                    district_id,
                    wuye_id,
                    current_page,
                    limit);
            throw new WrenchException(Result.QUERY_USER_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_BY_DANYUAN_ID_FAIL_MESSAGE);
        }

        logger.debug("==> userList=dao.queryUserByDistrictId(district_id, (Integer.valueOf(current_page)-1)*Integer.valueOf(limit), Integer.valueOf(limit)) execute success  , userList = {}  , district_id = {} , current_page = {} , limit = {} , ",
                userList,
                district_id,
                wuye_id,
                current_page,
                limit);

        int total = -1;

        try {
            total = dao.queryUserByDistrictIdCount(district_id,wuye_id);
        } catch (Exception e) {
            logger.error("==> total=dao.queryUserByDistrictIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {} , error = {} ",
                    district_id,
                    wuye_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (total < 0) {
            logger.error("==> total=dao.queryUserByDistrictIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {} ",
                    district_id,
                    wuye_id);
            throw new WrenchException(Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_CODE,
                    Result.QUERY_USER_COUNT_BY_DANYUAN_ID_FAIL_MESSAGE);
        }
        logger.error("==> total=dao.queryUserByCityIdCount(city_id,wuye_id) execute error , city_id = {} , wuye_id = {}  ",
                district_id,
                wuye_id
        );

        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(userList);
        return page;

    }
    
    
    /**
     * 用户审核通过进行推送     flag 传 1
     **/
    public List<String> getDeviceAlias(String user_id, int flag)
        throws WrenchException {
    if (WrenchUtil.hasNull(user_id)) {
        logger.error("==> param is not found , user_id = {} ", user_id);
        throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,
                Result.PARAM_IS_NOT_FOUND_MESSAGE);
    }

    if (WrenchUtil.stringIsBlank(user_id)) {
        logger.error("==> string is blank , user_id = {} ", user_id);
        throw new WrenchException(Result.STRING_IS_BLANK_CODE,
                Result.STRING_IS_BLANK_MESSAGE);
    }

    List<String> device_alias = null;
    try {
        device_alias = dao.getDeviceAlias(user_id, flag);
    } catch (Exception e) {
        logger.error(
                "==> device_alias=dao.getDeviceAlias(user_id,flag) execute error , user_id = {} , flag = {} , error = {} ",
                user_id, flag, e);
        throw new WrenchException(Result.DB_ERROR_CODE,
                Result.DB_ERROR_MESSAGE);
    }

    if (WrenchUtil.hasNull(device_alias)) {
        logger.error(
                "==> device_alias=dao.getDeviceAlias(user_id,flag) execute  success but device_alias  is null , device_alias = {} , flag = {} , user_id = {} ",
                device_alias, flag, user_id);
        throw new WrenchException(
                Result.GET_JIAOXIANG_DEVICE_ALAIS_FAIL_CODE,
                Result.GET_JIAOXIANG_DEVICE_ALAIS_FAIL_MESSAGE);
    }
    logger.debug(
            "==> device_alias=dao.getDeviceAlias(user_id,flag) execute  success , device_alias.size( = {} , user_id = {}  , flag = {}",
            device_alias.size(), user_id, flag);
    return device_alias;

}
    
    /**
     * flag=1 取最近一条记录 否则全部                    审核通过后进行推送
     */
    public List<Map<String, String>> getFaceIds(String user_id, int flag)
            throws WrenchException {
        if (WrenchUtil.hasNull(user_id)) {
            logger.error("==> param is not found , user_id = {} ", user_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);

        }
        List<Map<String, String>> faces = null;
        try {
            faces = dao.getFaceIds(user_id, flag);
        } catch (Exception e) {
            logger.error("==> faces = dao.getFaceIds(user_id,flag) execute error , flag = {} ,  user_id = {} ,error = {}   ",
                    flag,
                    user_id,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(faces)) {
            logger.error("==> faces = dao.getFaceIds(user_id)  execute success , flag = {} , user_id = {} ,faces = {}  ",
                    flag,
                    user_id,
                    faces);
            throw new WrenchException(Result.GET_FACES_FAIL_CODE, Result.GET_FACES_FAIL_MESSAGE);
        }

        return faces;
    }

    
    /**
     * 根据电话获取用户id               
     */
    public String getUseridByTel(String telphone)
            throws WrenchException {
        if (WrenchUtil.hasNull(telphone)) {
            logger.error("==> param is not found , telphone = {} ", telphone);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);

        }
        String user_id = null;
        try {
            user_id = dao.getUseridByTel(telphone);
            
        } catch (Exception e) {
            logger.error("==> faces = dao.getUseridByTel(telphone) execute error , telphone = {} ,error = {}   ",
                telphone,
                    e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }

        if (WrenchUtil.hasNull(user_id)) {
            logger.error("==> tel = dao.getFaceIds(user_id)  execute success , user_id = {}   ",
                user_id
                    );
            throw new WrenchException("100", "get user_id fail by telphon");
        }

        return user_id;
    }
    
    
}
